package com.wx.controller;

import com.wx.dto.ForgotPwdDTO;
import com.wx.dto.RefineUserInfo;
import com.wx.dto.UpdatePwd;
import com.wx.dto.UserUpdateDTO;
import com.wx.entity.User;
import com.wx.result.Result;
import com.wx.service.UserService;
import com.wx.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*允许跨域请求*/
@CrossOrigin
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    /*用户注册账号*/
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        log.info("用户注册：{}",user);
        userService.register(user);
        return Result.success();
    }

    /*用户登录*/
    @GetMapping("/login")
    public Result login(User user){
        log.info("用户登录：{}",user);
        UserLoginVO userLoginVO = userService.login(user);
        return Result.success(userLoginVO);
    }

    /*用户修改信息*/
    @PostMapping("/update")
    public Result update(@RequestBody UserUpdateDTO userUpdateDTO){
        log.info("用户修改信息：{}",userUpdateDTO);
        userService.update(userUpdateDTO);
        return Result.success();
    }

    /*用户修改密码*/
    @PostMapping("/updatepwd")
    public Result updatePwd(@RequestBody UpdatePwd pwd){
        log.info("用户修改密码：{}",pwd);
        userService.updatePwd(pwd);
        return Result.success();
    }

    /*用户忘记密码，重置密码*/
    @PostMapping("/forgetpwd")
    public Result forgotpwd(@RequestBody ForgotPwdDTO forgotPwdDTO){
        log.info("用户重置密码：{}",forgotPwdDTO);
        userService.forgotpwd(forgotPwdDTO);
        return Result.success();
    }

    /*用户完善个人信息*/
    @PostMapping("/refineinfo")
    public Result refine(@RequestBody RefineUserInfo refineUserInfo){
        log.info("用户完善个人信息：{}",refineUserInfo);
        userService.refine(refineUserInfo);
        return Result.success();
    }

    /*用户注销账号*/
    @DeleteMapping("/logout")
    public Result deleteUser(User user){
        log.info("用户注销账号：{}",user);
        userService.deleteUser(user);
        return Result.success();
    }

    /*清除用户购物车和收藏数据*/
    @GetMapping("/purge")
    public Result purge(User user){
        log.info("清除用户购物车和收藏数据:{}",user);
        userService.purge(user);
        return Result.success();
    }

}
